package com.jt.aop;

import com.jt.pojo.User;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Aspect
@Component
public class SpringAOP {


    Map<Integer,Object> map = new HashMap<>();

    @Around("execution(* com.jt..*.*(..))")
    public Object around(ProceedingJoinPoint joinPoint){
        Object result=null;
        System.out.println("执行环绕通知");
        Object[] args = joinPoint.getArgs();
        User user = (User) args[0];
        Integer id = user.getId();
        if (map.containsKey(id)){
            System.out.println("缓存获取");
            return map.get(id);
        }else{
            try {
                System.out.println("目标方法获取保存");
                result = joinPoint.proceed();
                map.put(id, user);
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }

        return result;
    }
}
